package my.jwds.p388;

import java.util.ArrayDeque;
import java.util.Deque;

public class LengthLongestPath {

    public static void main(String[] args) {

        Solution obj = new Solution();
        long start = System.currentTimeMillis();
        System.out.println(obj.lengthLongestPath("dir\n\tsubdir1\n\t\tfile1.ext\n\t\tsubsubdir1\n\tsubdir2\n\t\tsubsubdir2\n\t\t\tfile2.ext"

               ));
        System.out.println("运行时间:" + (System.currentTimeMillis() - start));
    }

    static class Solution {


        public int lengthLongestPath(String input) {
            input+="\n";
            int max = 0;
            Deque<Integer> stack = new ArrayDeque<>();
            int tier = 0,len = 0;
            boolean isFile = false;
            for (int i = 0; i < input.length(); i++) {
                char c = input.charAt(i);
                if (c == '\n'){
                    while (tier < stack.size()){
                        stack.pop();
                    }
                    int parentLen = -1;
                    if (!stack.isEmpty())parentLen = stack.peek();
                    stack.push(parentLen+1+len);
                    if (isFile && stack.peek() > max)max = stack.peek();
                    len = 0;
                    isFile = false;
                    tier = 0;
                }else if (c == '\t'){
                    tier ++;
                }else {
                    len ++;
                    if (c == '.')isFile = true;
                }

            }

            return max;
        }
    }
}